<?php
error_reporting(E_ALL);
$mysqli = new mysqli('MYSQL_HOST', 'MYSQL_USER', 'MYSQL_PASSWORD', 'MYSQL_DATABASE');
$limit = 2000; // how many issues to generate AHK script for?

// get last Issue ID from GoogleCode and +1
$page = file_get_contents("http://code.google.com/p/eathena/issues/list?can=1&sort=-id&num=1&colspec=ID");
preg_match('#detail\?id=([0-9]*)#', $page, $matches);
$start = intval($matches[1]) +1;

// get last Bug ID in SQL
if ($result = $mysqli->query("SELECT bug_id FROM bugtracker ORDER BY bug_id DESC LIMIT 1")) {
	$obj = $result->fetch_object();
	$end = $obj->bug_id;
	$result->close();
} else {
	// no bugs in SQL? run bugtracker2sql.php first
	$mysqli->close();
	exit;
}

if ($start == $end) {
	// nothing to do
	$mysqli->close();
	exit;
}

$status = array('UNFILED' => 'New',
                'Confirmed' => 'Confirmed',
                'Working as Intended' => 'WorkingAsIntended',
                'Unable to Fix' => 'UnableToFix',
                'Fixed' => 'Fixed',
                'Duplicate' => 'Duplicate');
$priority = array('Low', 'Medium', 'High', 'Critical');

header("Content-Type: text/plain");
echo "^!i Up:: ; Ctrl+Alt+I, release
KeyWait, Control
KeyWait, Alt
KeyWait, i

; NOTE: coordinates were generated in Google Chrome 13.0.782.99 beta
CoordMode, Mouse, Relative
SetTitleMatchMode 3
sleep 2000

\n";

if ($result = $mysqli->query(sprintf("SELECT bug_id,summary,`status`,priority,branch,component FROM bugtracker ORDER BY bug_id ASC LIMIT %d OFFSET %d", $limit,$start-1))) {
	while ($obj = $result->fetch_object()) {
		if ($obj->summary == '') {
			echo sprintf("; BUG ID %d
Click 120,90 ; http://code.google.com/p/eathena/issues/entry?template=Bug Report NOT FOUND
Sleep 1000
Click 150,325 ; Summary
Send ^a
SendInput (bug report not found)
Click 634,357 ; Description
Send {Backspace}
SendInput %d
Send ^+{End}
Send {Delete}
", $obj->bug_id, $obj->bug_id);
		} else {
			echo sprintf("; BUG ID %d
Click 35,90 ; http://code.google.com/p/eathena/issues/entry
Sleep 1000
Click 150,325 ; Summary
Send ^a
SendRaw %s
Click 634,357 ; Description
Send {Backspace}
SendInput %d
Send ^+{End}
Send {Delete}
Click 150,643 ; Status
Send ^a
SendInput %s
Click 344,737 ; Priority
Send ^a
SendInput Priority-%s
", $obj->bug_id, str_replace(array('`','%',';','::'),array('``','`%','`;','`::'),$obj->summary), $obj->bug_id, $status[$obj->status], $priority[$obj->priority]);
			
			
			switch ($obj->branch) {
			case 'Stable-Old':
				$branch = 'StableOld';
				break;
			case 'Stable':
				$branch = 'Stable';
				break;
			case 'Trunk':
				$branch = 'Trunk';
				break;
			case 'Renewal':
				$branch = 'Renewal';
				break;
			case 'Stable & Trunk':
				$branch = 'Stable & Trunk';
				break;
			case 'All':
				$branch = 'All';
				break;
			default:
				$branch = '';
				break;
			}
			
			switch ($obj->component) {
			case 'eAthena':
				$component = 'Core';
				break;
			case 'eAthena Skills':
				$component = 'Skills';
				break;
			case 'eAthena Scripts':
				$component = 'Scripts';
				break;
			case 'eAthena Databases':
				$component = 'Databases';
				break;
			case 'Core':
				$branch = 'Renewal';
				$component = 'Core';
				break;
			case 'Skills':
				$branch = 'Renewal';
				$component = 'Skills';
				break;
			case 'Scripts':
				$branch = 'Renewal';
				$component = 'Scripts';
				break;
			case 'Database':
				$branch = 'Renewal';
				$component = 'Databases';
				break;
			case 'Development Tasks & Progress':
				$branch = 'Renewal';
				$component = 'TODO';
				break;
			case 'eAthena Bug Archives':
				$component = '';
				break;
			case 'eAPP':
				$branch = 'eAPP';
				$component = 'FIX_ME';
				break;
			default:
				$component = 'FIX_ME';
				break;
			}
			
			
			echo sprintf("Click 150,737 ; Branch
Send ^a
SendInput %s
Click 60,763
Click 150,763 ; Component
Send ^a
SendInput %s
", 
			(($branch == '') ? "{Delete}" : ("Branch-" . $branch)), 
			(($component=='')? "{Delete}":("Component-".$component)));
		}
		echo "Click 40,846 ; submit !!
WinWaitActive, Issues - eathena - eAthena - MMORPG server package - Google Project Hosting - Google Chrome
\n";
	}
	$result->close();
}

echo "\nreturn\n\n";

$mysqli->close();
exit;
?>
